package com.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.controller.base.BaseController;
import com.entity.User;
import com.service.UserService;

@Controller
@RequestMapping("/download")
public class DownloadController extends BaseController<Object>{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/user")  
    public ResponseEntity<?> export(String id){  
		Map<String,Object> respMap = new HashMap<String,Object>();
		ZipOutputStream zipOut = null;
		File tempZip = null;
		try{
	    	User user = new User();
	    	user.setId(id);
	    	user.setDetail(true);
			user.setFileRemark("normal");
			List<User> users = userService.find(user);
			if(users != null && users.size() > 0){
				String userName = users.get(0).getUserName();
				//需要压缩的文件
				List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
				for (User u : users) {
					String url = u.getFilePath() + u.getFileFile();
					url = url.substring(url.indexOf("SpringDemo") + 11);
					Map<String, String> map = new HashMap<String, String>();
					map.put("url", url);
					map.put("name", u.getFileName());
					maps.add(map);
				}
				//压缩后的文件
				String resourcesName =  userName + ".zip";
				String realPath = getRequest().getSession().getServletContext().getRealPath("/");
				zipOut = new ZipOutputStream(new FileOutputStream(realPath + "upload\\" +  resourcesName));
				tempZip = new File(realPath + "upload\\" +  resourcesName);
				InputStream input = null;
				for (Map<String, String> map : maps) {
					String name = realPath + map.get("url");
					input = new FileInputStream(new File(name));  
		            zipOut.putNextEntry(new ZipEntry( map.get("name")));  
		            int temp = 0;  
		            while((temp = input.read()) != -1){  
		                zipOut.write(temp);  
		            }  
		            input.close();
				}
				zipOut.close();
				//自动删除临时文件
				HttpHeaders headers = new HttpHeaders();
				String filename = new String(resourcesName.getBytes("utf-8"),"iso-8859-1");
				headers.setContentDispositionFormData("attachment", filename);
				headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
				return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(tempZip),headers,HttpStatus.CREATED);
			}
			respMap.put("resMsg", "该用户未上传附件");
		}catch(IOException e){
			respMap.put("resMsg", "下载失败");
		}finally{
			try {
				if(zipOut != null)zipOut.close();
				if(tempZip != null){
					if(tempZip.exists()){
						tempZip.delete();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new ResponseEntity<Map<String,Object>>(respMap, HttpStatus.OK);
    }

}
